/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class BinarySearchFloat {
    public static double ComplexFloatNumeric() {
        int count = 5000000;
        double[] testArray = new double[]{2.3, 4.6, 5.2, 5.5, 6.1, 6.7, 7.4, 7.6, 7.886, 8.2, 9.11, 10.02, 33.2};

        int res = 5;
        double start = System.nanoTime();
        int testArrayLength = testArray.length;
        for (int i = 1; i < count; i++){
            double value = testArray[i % res & (testArrayLength - 1)];
            int tmp = 0;
            int low = 0;
            int high = testArrayLength - 1;
            int middle = high >>> 1;
            for (; low <= high; middle = (low + high) >>> 1) {
                double test = testArray[middle];
                if (test > value) {
                    high = middle - 1;
                } else if (test < value) {
                    low = middle + 1;
                } else {
                    tmp = middle;
                    break;
                }
            }
            res += tmp;
        }
        double end = System.nanoTime();
        System.out.println("" + res);
        double time = (end - start)  / 1000000;
        System.out.println("Numerical Calculation - ComplexFloatNumeric:\t"+time+"\tms");
        return time;
    }
}

class Benchmark {
    public static void main(String[] args) {
        BinarySearchFloat.ComplexFloatNumeric();
    }
}